<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="请求模块" prop="module">
        <el-input
          v-model="queryParams.module"
          placeholder="请输入请求模块"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求地址" prop="path">
        <el-input
          v-model="queryParams.path"
          placeholder="请输入请求地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求方法" prop="method">
        <y-select
          v-model="queryParams.method"
          placeholder="请选择请求方法"
          :options="sys_http_method"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <y-select
          v-model="queryParams.status"
          placeholder="请选择操作状态"
          :options="sys_success_fail"
        />
      </el-form-item>
      <el-form-item label="操作时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 对象操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys:operlog:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleClean"
          v-hasPermi="['sys:operlog:delete']"
          >清空
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <y-export
          :url="'sys/operlog/export/'"
          :ids="ids"
          :filename="'operlog_data'"
          v-hasPermi="['sys:operlog:export']"
        />
      </el-col>
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!-- 列表展示 -->
    <el-table
      ref="tableRef"
      v-loading="loading"
      row-key="id"
      :data="operlogList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="编号"
        align="center"
        prop="id"
        width="300"
        v-if="columns[0].visible"
        sortable
      />
      <el-table-column
        label="请求模块"
        align="center"
        prop="module"
        :show-overflow-tooltip="true"
        v-if="columns[1].visible"
        sortable
      />
      <el-table-column
        label="请求地址"
        align="center"
        prop="path"
        :show-overflow-tooltip="true"
        v-if="columns[2].visible"
        sortable
      />
      <el-table-column
        label="请求方法"
        align="center"
        prop="method"
        :show-overflow-tooltip="true"
        v-if="columns[3].visible"
        sortable
      >
        <template #default="scope">
          <y-dict-tag :options="sys_http_method" :value="scope.row.method" />
        </template>
      </el-table-column>
      <el-table-column
        label="IP地址"
        align="center"
        prop="ip"
        :show-overflow-tooltip="true"
        v-if="columns[4].visible"
        sortable
      />
      <el-table-column
        label="请求浏览器"
        align="center"
        prop="browser"
        :show-overflow-tooltip="true"
        v-if="columns[5].visible"
        sortable
      />
      <el-table-column
        label="响应码"
        align="center"
        prop="code"
        :show-overflow-tooltip="true"
        v-if="columns[6].visible"
        sortable
      />
      <el-table-column
        label="返回消息"
        align="center"
        prop="result"
        :show-overflow-tooltip="true"
        v-if="columns[7].visible"
        sortable
      />
      <el-table-column
        label="操作人"
        align="center"
        prop="creator_name"
        :show-overflow-tooltip="true"
        v-if="columns[8].visible"
        sortable
      />
      <el-table-column
        label="操作状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
        v-if="columns[9].visible"
        sortable
      >
        <template #default="scope">
          <y-dict-tag :options="sys_success_fail" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作日期"
        align="center"
        prop="create_time"
        width="180"
        v-if="columns[9].visible"
        sortable
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template #default="scope">
          <el-tooltip content="详细" placement="top">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handleView(scope.row, scope.index)"
              v-hasPermi="['sys:operlog:detail']"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" v-model="open" width="700px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.module }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作方法：">
              <y-dict-tag :options="sys_http_method" :value="form.method" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求地址：">{{ form.path }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ form.body }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <y-dict-tag :options="sys_success_fail" :value="form.status" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{
              parseTime(form.update_time)
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="form.status === 1">{{
              form.errorMsg
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Operlog">
import { listOperlog, delOperlog, cleanOperlog } from "@/api/sys/operlog";

const { proxy } = getCurrentInstance();
const { sys_http_method, sys_success_fail } = proxy.useDict(
  "sys_http_method",
  "sys_success_fail"
);

const operlogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const defaultSort = ref({ prop: "operTime", order: "descending" });

// 列显隐信息
const columns = ref([
  { key: 0, label: `编号`, visible: false },
  { key: 1, label: `请求模块`, visible: true },
  { key: 2, label: `请求地址`, visible: true },
  { key: 3, label: `请求方法`, visible: true },
  { key: 4, label: `IP地址`, visible: true },
  { key: 5, label: `请求浏览器`, visible: true },
  { key: 6, label: `响应码`, visible: true },
  { key: 7, label: `返回消息`, visible: true },
  { key: 8, label: `操作人`, visible: true },
  { key: 9, label: `操作状态`, visible: true },
  { key: 10, label: `操作日期`, visible: true },
]);

const data = reactive({
  form: {},
  queryParams: {
    module: undefined,
    path: undefined,
    businessType: undefined,
    status: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询登录日志 */
function getList() {
  loading.value = true;
  listOperlog(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      operlogList.value = response.data;
      total.value = response.total;
      loading.value = false;
    }
  );
}
/** 操作日志类型字典翻译 */
function typeFormat(row, column) {
  return proxy.selectDictLabel(sys_http_method.value, row.businessType);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  queryParams.value.page = 1;
  proxy.resetForm("queryRef");
  proxy.$refs.tableRef.sort(defaultSort.value.prop, defaultSort.value.order);
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}
/** 详细按钮操作 */
function handleView(row) {
  open.value = true;
  form.value = row;
}
/** 删除按钮操作 */
function handleDelete(row) {
  ids.value = row.id ? [row.id] : ids.value;
  proxy.$modal
    .confirm('是否确认删除日志编号为"' + ids.value + '"的数据项?')
    .then(function () {
      return delOperlog(ids.value);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有操作日志数据项?")
    .then(function () {
      return cleanOperlog();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}

onMounted(() => {
  getList();
});
</script>
